package com.ask.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 配置异步任务执行器
 */
@Configuration
@EnableAsync // 启用异步支持
public class AsyncConfig {
    @Bean("orderTaskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 线程池任务执行器
        executor.setCorePoolSize(10); // 核心线程数
        executor.setMaxPoolSize(16); // 最大线程数
        executor.setQueueCapacity(200); // 队列容量
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 更改拒绝策略
        executor.setThreadNamePrefix("Order-Async-"); // 线程名前缀
        executor.initialize();
        return executor;
    }
}
